﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class CompetitorsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Competitors/ZTSDU
        public ViewResult Index(string clubId,  string searchstring)
        {
            var competitors = db.Competitors.Where(c=>c.ClubId == clubId).OrderBy(c => c.Surname).Include("Clubs").Include("Countries").ToList();

            if (!string.IsNullOrEmpty(searchstring))
            {
                competitors = competitors.Where(c => c.Surname.Contains(searchstring)).ToList();
            }

            ViewBag.Clubs = db.Clubs.Single(c => c.ID == clubId);
            return View(competitors);
        }

        // GET: /Competitors/Details/5
        public ViewResult Details(int id)
        {
            Competitors competitors = db.Competitors.Single(c => c.ID == id);
            ViewBag.Clubs = db.Clubs.Single(c => c.ID == competitors.ClubId);
            return View(competitors);
        }

        // GET: /Competitors/Create/ZTSDU
        public ActionResult Create(string clubId)
        {
            ViewBag.Clubs = db.Clubs.Single(c => c.ID == clubId);
            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", clubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", "SVK");
            return View();
        } 

        // POST: /Competitors/Create
        [HttpPost]
        public ActionResult Create(Competitors competitors)
        {
            if (ModelState.IsValid)
            {
                db.Competitors.AddObject(competitors);
                db.SaveChanges();
                return RedirectToAction("Index", new { clubId = competitors.ClubId });  
            }

            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }
        
        // GET: /Competitors/Edit/5
        public ActionResult Edit(int id)
        {
            Competitors competitors = db.Competitors.Single(c => c.ID == id);
            ViewBag.Clubs = db.Clubs.Single(c => c.ID == competitors.ClubId);
            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }

        // POST: /Competitors/Edit/5
        [HttpPost]
        public ActionResult Edit(Competitors competitors)
        {
            if (ModelState.IsValid)
            {
                db.Competitors.Attach(competitors);
                db.ObjectStateManager.ChangeObjectState(competitors, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { clubId = competitors.ClubId });
            }
            ViewBag.ClubId = new SelectList(db.Clubs, "ID", "Name", competitors.ClubId);
            ViewBag.CountryId = new SelectList(db.Countries, "ID", "Name", competitors.CountryId);
            return View(competitors);
        }

        // GET: /Competitors/Delete/5
        public ActionResult Delete(int id)
        {
            Competitors competitors = db.Competitors.Single(c => c.ID == id);
            return View(competitors);
        }

        // POST: /Competitors/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Competitors competitors = db.Competitors.Single(c => c.ID == id);
            db.Competitors.DeleteObject(competitors);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}